package com.entitymapper.field;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.entitymapper.driver.Driver;

public abstract class AbstractResultSet implements ResultSet {

	protected Driver driver;
	protected Class<?> entityType;
	
	public AbstractResultSet(Class<?> entityType) {
		this.entityType = entityType;
	}
	
	protected abstract String readValue(String fieldName);
	
	protected abstract void writeValue(String fieldName, Object object);
	
	@Override
	public int getInt(String fieldName) {
		return Integer.parseInt(readValue(fieldName));
	}

	@Override
	public String getString(String fieldName) {
		return readValue(fieldName);
	}

	@Override
	public long getLong(String fieldName) {
		return Long.parseLong(readValue(fieldName));
	}

	@Override
	public Date getDate(String fieldName){
		String timeValue = readValue(fieldName);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date time = null;
		try {
			time = sdf.parse(timeValue);
			return time;
		} catch (ParseException e) {
			return null;
			//throw new EntityMapperException("String transport to date type has error, because :",e);
		}
	}

	@Override
	public void setValue(String fieldName,Object fieldValue) {
		writeValue(fieldName,fieldValue);
	}
	
	@Override
	public void setDate(String fieldName, Date time) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		writeValue(fieldName, sdf.format(time));
	}
	
	public Driver getDriver() {
		return driver;
	}
	public void setDriver(Driver driver) {
		this.driver = driver;
	}
}
